//
//  XGVideoPlayerViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/5/4.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import BMPlayer

class XGVideoPlayerViewController: UIViewController
{
    init(playVideoUrl: String)
    {
        self.playVideoUrl = playVideoUrl
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 控制器生命周期方法
    
    override func loadView()
    {
        super.loadView()
        setUpUI()
    }
    
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
        guard let url = URL(string: playVideoUrl) else {
            return
        }
        
        let playResource = BMPlayerResource(url: url)
        player.setVideo(resource: playResource)
    }
    
    override func viewWillDisappear(_ animated: Bool)
    {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    // MARK: - 私有属性
    /// 播放地址
    private let playVideoUrl: String
    /// 播放器
    private lazy var player: BMPlayer = { [weak self] in
        let player = BMPlayer()
        player.delegate = self
        return player
    }()
}

extension XGVideoPlayerViewController: BMPlayerDelegate
{
    func bmPlayer(player: BMPlayer, playerOrientChanged isFullscreen: Bool) {
        player.snp.remakeConstraints { (make) in
            make.centerY.equalTo(view)
            make.left.equalTo(view.snp.left)
            make.right.equalTo(view.snp.right)
            if isFullscreen {
                make.bottom.equalTo(view.snp.bottom)
            } else {
                make.height.equalTo(view.snp.width).multipliedBy(9.0/16.0).priority(500)
            }
        }
    }
    
    func bmPlayer(player: BMPlayer, playerStateDidChange state: BMPlayerState) {}
    func bmPlayer(player: BMPlayer, loadedTimeDidChange loadedDuration: TimeInterval, totalDuration: TimeInterval) {}
    func bmPlayer(player: BMPlayer, playTimeDidChange currentTime : TimeInterval, totalTime: TimeInterval) {}
    func bmPlayer(player: BMPlayer, playerIsPlaying playing: Bool) {}
}

// MARK: - 私有扩展

private extension XGVideoPlayerViewController
{
    func setUpUI() -> Void
    {
        view.backgroundColor = UIColor.black
        view.addSubview(player)
        player.snp.makeConstraints { (make) in
            make.centerY.equalTo(view)
            make.left.equalTo(view)
            make.width.equalTo(view)
            // 注意此处，宽高比 16:9 优先级比 1000 低就行，在因为 iPhone 4S 宽高比不是 16：9
            make.height.equalTo(player.snp.width).multipliedBy(9.0 / 16.0).priority(750)
        }
        player.backBlock = { [unowned self] (isFullScreen) in
            if isFullScreen {
                return
            }
            
            let _ = self.navigationController?.popViewController(animated: true)
        }
    }
}
